package com.cloudxingkong.dianyinke.Student.Adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Ui.CreditsExchangeDetails;
import com.cloudxingkong.dianyinke.Student.bean.CETest;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by 云星空科技 on 2016/3/22.
 * 积分兑换Adapter
 */
public class CreditsExchangeGVAdapter extends BaseAdapter {
    private Context context;
    private List<CETest.ListBean> list;
    public CreditsExchangeGVAdapter(Context context, List<CETest.ListBean> list) {
        this.context = context;
        this.list = list;

    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list==null?null:list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
             final ViewHolder holder;
        if (convertView == null) {
            convertView=View.inflate(context, R.layout.creditsexchange_item,null);
            holder=new ViewHolder();
            holder.btnDuiHuan= (TextView) convertView.findViewById(R.id.ce_btn_duihuan);
            holder.btnJia= (ImageView) convertView.findViewById(R.id.ce_btn_jia);
            holder.btnJian= (ImageView) convertView.findViewById(R.id.ce_btn_jian);

            holder.ivCover= (ImageView) convertView.findViewById(R.id.ce_cover);//图片
            holder.tvName= (TextView) convertView.findViewById(R.id.ce_tv_name);//名字
            holder.tvNum= (TextView) convertView.findViewById(R.id.ce_tv_num);//个数
            holder.tvShengYu= (TextView) convertView.findViewById(R.id.ce_tv_shengyu);//商品剩余
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
//        int price = list.get(position).getPrice();//单价

        holder.tvName.setText(list.get(position).getName() + "");//商品名称
        holder.tvShengYu.setText(list.get(position).getPoints() + "");//商品剩余

        ImageLoader.getInstance().displayImage(list.get(position).getPic(), holder.ivCover);

        holder.tvNum.setText(holder.NUM_CURRENT + "");
        final ViewHolder finalHolder = holder;

        holder.btnJia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                finalHolder.NUM_CURRENT++;
                if (finalHolder.NUM_CURRENT >list.get(position).getPoints()) {
                    Toast.makeText(context, "包圆儿啦~", Toast.LENGTH_SHORT).show();
                    return;
                }
                finalHolder.tvNum.setText(finalHolder.NUM_CURRENT+"");
                Log.e("test","商品数量增加+++>> "+finalHolder.NUM_CURRENT+" 设置数量 "+finalHolder.tvNum.getText());
            }
        });
        holder.btnJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (finalHolder.NUM_CURRENT <= 0) {
                    return;
                }
                --finalHolder.NUM_CURRENT;
                finalHolder.tvNum.setText(finalHolder.NUM_CURRENT + "");
                Log.e("test", "商品数量减少---->> " + finalHolder.NUM_CURRENT+" 设置数量 "+finalHolder.tvNum.getText() );
            }
        });

        holder.btnDuiHuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("test", "积分兑换>> " + finalHolder.tvNum.getText());
                if (finalHolder.NUM_CURRENT == 0) {
                    Toast.makeText(context,"您还没选择数量",Toast.LENGTH_SHORT).show();
                }else{
                    new AlertDialog.Builder(context)
                            .setTitle("积分兑换")
                            .setMessage("亲、您确定要兑换吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                    Toast.makeText(context, "请确认订单", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(context,CreditsExchangeDetails.class);
                                    intent.putExtra("num",holder.tvNum.getText()+"");//选择个数
                                    intent.putExtra("goodsID",list.get(position).getId()+"");//商品id
                                    intent.putExtra("goodsPic",list.get(position).getPic()+"");//商品照片
                                    intent.putExtra("goodsTitle",list.get(position).getName()+"");//商品名称
                                    intent.putExtra("goodsGuige",list.get(position).getSize()+"");//规格
                                    intent.putExtra("goodsAsTime",list.get(position).getTs()+"");//发货时间
                                    intent.putExtra("points",list.get(position).getPoints()+"");//积分
                                    context.startActivity(intent);
                                }
                            })
                            .setNegativeButton("返回", null).show();//在按键响应事件中显示此对话框
                }
            }
        });

        return convertView;
    }
    private class ViewHolder{
        TextView tvName,tvNum,tvShengYu,btnDuiHuan;
        ImageView ivCover,btnJia,btnJian;
        int NUM_CURRENT=1;
    }
}
